package lar.system.network;

import java.util.AbstractQueue;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import lar.system.engine.MainProgram;
import lar.system.network.msgParser.ParsedMessage;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public abstract class Listener 
{

	protected ConcurrentLinkedQueue<ParsedMessage> Q;			//shared queue by UDP and TCP Listeners
	
	public abstract void shutDown();
	
	public Listener()
	{
		Q = new ConcurrentLinkedQueue<ParsedMessage>();
	}
	
	public Listener(ConcurrentLinkedQueue<ParsedMessage> q)
	{
		Q = q;
	}
	
	public boolean isQueueEmpty()
	{
		boolean ans;
		synchronized(Q) { ans = Q.isEmpty(); }
		return ans;
	}
	
	public ParsedMessage getIncomingMessage()
	{
		ParsedMessage ans;
		synchronized (Q) { ans = Q.poll(); }
		return ans;
	}

	
	public void pushBackMessage(ParsedMessage msg)
	{
		synchronized(Q) { Q.add(msg); }
	}
	
	public static String myIP()
	{
		WifiManager mWifiManager = (WifiManager) MainProgram.currentActivity.getSystemService(Context.WIFI_SERVICE);
		WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
		return intToIP(mWifiInfo.getIpAddress());
	}

	public static String intToIP(int i) 
	{
		return (( i & 0xFF)+ "."+((i >> 8 ) & 0xFF)+"."+((i >> 16 ) & 0xFF)+"."+((i >> 24 ) & 0xFF));
	} 
	
}
